package com.haiyou.common.aspect;

import com.haiyou.common.anno.CountTime;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class OptimizeLogAspect {

    @Around("@annotation(countTime)")
    public Object around(ProceedingJoinPoint point, CountTime countTime) throws Throwable {
        //开始时间
        long start = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        long end = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) point.getSignature();
        //请求的方法名
        String className = point.getTarget().getClass().getName();
        String methodName = signature.getName();
        log.info("【接口执行时间】接口名：{}.{},执行时间:{}毫秒", className, methodName, (end - start));
        return result;
    }

}